package com.atguigu;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
@WebServlet("/servlet1")
public class servlet1 extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收请求中的username参数
        String username=req.getParameter("username");
        //获取session对象
        HttpSession session = req.getSession();
        //判断请求中有没有一个特殊的cookie JSESSIONID
        //1.有    根据IESSIONID找对应的session对象  找到了，返回之前的session

                //没有找到对应的session对象，创建一个新的session对象，并且将response对象中存放一个JESSIONID的cookie
        //2.没有  创建一个新的session返回，并且将response对象中存放一个JESSIONID的cookie
        System.out.println(session.getId());
        System.out.println(session.isNew());

        //将username参数存入session对象中
        session.setAttribute("username",username);
        //客户端响应信息
        resp.setContentType("text/html,charset=utf-8");
        resp.getWriter().write("成功");
    }
}
